package com.zyw.zhuangdian.service.impl;

import com.zyw.zhuangdian.constant.ServiceConstant;
import com.zyw.zhuangdian.dao.MessageDao;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.dto.PageDto;
import com.zyw.zhuangdian.pojo.in.MessageDtoIn;
import com.zyw.zhuangdian.pojo.out.MessageDtoOut;
import com.zyw.zhuangdian.property.MyProperties;
import com.zyw.zhuangdian.service.MessageService;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class MessageServiceImpl implements MessageService {
    private final Logger logger = LoggerFactory.getLogger(MessageServiceImpl.class);

    private final MessageDao MessageDao;

    private final MyProperties properties;

    @Override
    public Resp<Void> addMessage(MessageDtoIn in) {
        Resp<Void> resp = new Resp<>();

        MessageDao.addMessage(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Void> updateMessage(MessageDtoIn in) {
        Resp<Void> resp = new Resp<>();

        MessageDao.updateMessage(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Void> deleteMessage(MessageDtoIn in) {
        Resp<Void> resp = new Resp<>();

        in.setStatus(ServiceConstant.STATUS_INVALID);

        MessageDao.deleteMessage(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Void> readMessages(MessageDtoIn in) {
        // 通过在已读表中增加记录标记已读

        Resp<Void> resp = new Resp<>();

        MessageDao.readMessages(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Void> unreadMessages(MessageDtoIn in) {
        Resp<Void> resp = new Resp<>();

        MessageDao.unreadMessages(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Integer> listUnreadMessageCount(MessageDtoIn in) {
        Resp<Integer> resp = new Resp<>();

        resp.setData(MessageDao.listUnreadMessageCount(BeanConvertUtil.beanCovertMap(in)));
        return resp;
    }

    @Override
    public Resp<List<MessageDtoOut>> listUnreadMessages(MessageDtoIn in) {
        Resp<List<MessageDtoOut>> resp = new Resp<>();
        if (in.getPage() != PageDto.DEFAULT_PAGE){
            if (in.getRow() == PageDto.DEFAULT_PAGE){
                in.setPage(PageDto.DEFAULT_PAGE);
            }else{
                in.setPage((in.getPage() - 1) * in.getRow());
            }
        }
        int count = MessageDao.listUnreadMessageCount(BeanConvertUtil.beanCovertMap(in));
        if (count > 0){
            List<Map> messages = MessageDao.listUnreadMessages(BeanConvertUtil.beanCovertMap(in));
            List<MessageDtoOut> dtoOuts = BeanConvertUtil.covertBeanList(messages,MessageDtoOut.class);
            resp.setData(dtoOuts);
        }

        resp.setTotal(count);
        if (in.getPage() != PageDto.DEFAULT_PAGE) {
            resp.setRecords((int) Math.ceil((double) count / (double) in.getRow()));
            resp.setPage(in.getPage());
            resp.setRow(in.getRow());
        }
        return resp;
    }

    @Override
    public Resp<Void> deleteAllMessage(MessageDtoIn in) {
        // 通过在已删除表中增加记录标记已删除
        Resp<Void> resp = new Resp<>();

        MessageDao.deleteAllMessage(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<List<MessageDtoOut>> listMessages(MessageDtoIn in) {
        Resp<List<MessageDtoOut>> resp = new Resp<>();
        if (in.getPage() != PageDto.DEFAULT_PAGE){
            if (in.getRow() == PageDto.DEFAULT_PAGE){
                in.setPage(PageDto.DEFAULT_PAGE);
            }else{
                in.setPage((in.getPage() - 1) * in.getRow());
            }
        }
        int count = MessageDao.listMessagesCount(BeanConvertUtil.beanCovertMap(in));
        if (count > 0){
            List<Map> messages = MessageDao.listMessages(BeanConvertUtil.beanCovertMap(in));
            List<MessageDtoOut> dtoOuts = BeanConvertUtil.covertBeanList(messages,MessageDtoOut.class);
            resp.setData(dtoOuts);
        }

        resp.setTotal(count);
        if (in.getPage() != PageDto.DEFAULT_PAGE) {
            resp.setRecords((int) Math.ceil((double) count / (double) in.getRow()));
            resp.setPage(in.getPage());
            resp.setRow(in.getRow());
        }
        return resp;
    }
}
